package fr.obsmip.sedoo.server.service;

import java.util.ArrayList;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import fr.obsmip.sedoo.client.service.GeonetworkObservatoryService;
import fr.obsmip.sedoo.core.RBVApplication;
import fr.obsmip.sedoo.core.dao.GeonetworkObservatoryDAO;
import fr.obsmip.sedoo.server.service.dtotool.GeonetworkObservatoryDTOTools;
import fr.obsmip.sedoo.shared.domain.GeonetworkObservatoryDTO;
import fr.sedoo.commons.client.util.ServiceException;

public class GeonetworkObservatoryServiceImpl extends RemoteServiceServlet implements GeonetworkObservatoryService{

	private GeonetworkObservatoryDAO dao;

	@Override
	public ArrayList<GeonetworkObservatoryDTO> findAll() throws ServiceException {
		initDao();
		return GeonetworkObservatoryDTOTools.toDTOList(dao.findAll());
	}

	@Override
	public void delete(GeonetworkObservatoryDTO observatory) throws ServiceException {
		initDao();
		dao.delete(GeonetworkObservatoryDTOTools.fromDTO(observatory));
	}

	@Override
	public GeonetworkObservatoryDTO create(GeonetworkObservatoryDTO observatory) throws ServiceException {
		initDao();		
		return GeonetworkObservatoryDTOTools.toDTO(dao.save(GeonetworkObservatoryDTOTools.fromDTO(observatory)));
	}
	
	private void initDao()
	{
		if (dao == null)
		{
			dao = RBVApplication.getInstance().getGeonetworkObservatoryDAO();
		}
	}
	
	@Override
	public GeonetworkObservatoryDTO edit(GeonetworkObservatoryDTO observatory) throws ServiceException {
		initDao();
		return GeonetworkObservatoryDTOTools.toDTO(dao.save(GeonetworkObservatoryDTOTools.fromDTO(observatory)));
	}

}
